package com.hnevc.acountms.activity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.FrameLayout;

import com.hnevc.acountms.R;

import org.litepal.crud.DataSupport;

public class TotalChartActivity extends AppCompatActivity {
    String passType ="";
    int[] colors = new int[]{Color.GREEN,Color.YELLOW, Color.RED
            ,Color.MAGENTA,Color.BLUE};

    public final int WIDTH=100;
    public final int HEIGHT=30;
    private final int OFFSET=50;
    private final float height=1000;
    public int x=70;
    public int y =1300;
    double[] monies;
    String[] types;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_total_chart);
        Intent intent = getIntent();
        passType  = intent.getStringExtra("type");
        initData();
        FrameLayout fl = (FrameLayout) findViewById(R.id.id_fl_chart);
        fl.addView(new MyView(this));

    }

    private void initData() {

        int i=0;
        Cursor cursor=null;

        if(passType.equals(ShowInfoActivity.IN_ACCOUNT)){
            //收入情况
            cursor= DataSupport
                    .findBySQL("select type,sum(money) as tmoney from Tb_inaccount group by type");
        }else{
            cursor = DataSupport
                    .findBySQL("select type,sum(money) as tmoney  from Tb_outaccount group by type");
        }
        int length = cursor.getCount();
        monies= new double[length];
        types = new String[length];
        cursor.moveToFirst();
        while(true){
            monies[i] = cursor.getDouble(1);
            types[i] = cursor.getString(0);
            i = i+1;
            if(!cursor.moveToNext()){
                break;
            }
        }
    }

    public static void actionStart(ShowInfoActivity context, String type) {
        Intent intent = new Intent(context,TotalChartActivity.class);
        intent.putExtra("type",type);
        context.startActivity(intent);


    }


    public class MyView extends View {

        public MyView(Context context) {
            super(context);
            
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.WHITE);
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            /*绘制坐标轴*/
            paint.setStrokeWidth(1);
            paint.setColor(Color.BLACK);
            canvas.drawLine(50,1300,900,1300,paint);
            canvas.drawLine(50,100,50,1300,paint);
            /*绘制柱型*/
            paint.setStyle(Paint.Style.FILL);
            int left = 0;

            double max = maxMoeny(monies);

            for (int i = 0; i < monies.length; i++) {
                paint.setColor(colors[i]);
                left = x + i*(OFFSET + WIDTH);
                canvas.drawRect(left,(float) (y-height/max* monies[i]),left+WIDTH,y,paint);
                paint.setTextSize(30);
                canvas.drawText(types[i],left+10,y+40,paint);
            }


        }
    }

    private double maxMoeny(double[] moneies) {
        double m=0;
        for (double money :
                moneies) {
            if(money>=m){
                m = money;
            }
        }
        return  m;
    }

}
